﻿using System;
using System.Collections.Generic;
using System.Web;

namespace dk.Helper.Base.Core
{
    public class PlayerParser : IPlayerParser
    {
        #region IPlayerParser Members

        /// <summary>
        /// Creates the player.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <param name="villages_in">The villages.</param>
        /// <returns></returns>
        public IPlayer CreatePlayer(string data, IVillages villages_in)
        {
            try
            {
                string[] array = data.Split(',');
                int id = Convert.ToInt32(array[0]);
                string name = HttpUtility.UrlDecode(array[1]);
                int allyID = Convert.ToInt32(array[2]);
                int villageCount = Convert.ToInt32(array[3]);
                int points = Convert.ToInt32(array[4]);
                int rank = Convert.ToInt32(array[5]);
                IList<IVillage> villages = null;
                if (villages_in.VillagesByPlayerID.ContainsKey(id))
                    villages = villages_in.VillagesByPlayerID[id];

                return new Player(id, name, allyID, villageCount, points, rank, villages);
            }
            catch (Exception)
            {
                return null;
            }
        }

        #endregion
    }
}